<?php 

namespace pmvc\mvc\security\facebook;

use pmvc\util\ClassUtil;
use pmvc\log\Log;
use pmvc\mvc\security\Authenticator;
use pmvc\mvc\security\Authentication;
use pmvc\mvc\Request;
use pmvc\mvc\Response;
use \Facebook;

class FacebookAuthenticator
	implements Authenticator {

	private $facebook;
	private $userAdapter;

	function authenticate(Authentication $authentication) {

		// check for a session
		$session = $this->facebook->getSession();
		if (!$session) {
			Log::error("No facebook session found");
			return NULL;
		}

		// check for a uid
		$uid = $this->facebook->getUser();
		if (!$uid) {
			Log::error("No facebook user found");
			return NULL;
		}

		// ok, time to return a user
		return $this->userAdapter->getUser($this->facebook, $authentication);
	}

	function supports(Authentication $authentication) {
		return ClassUtil::isInstance(
			$authentication, 'pmvc\mvc\security\facebook\FacebookAuthentication');
	}

	public function setUserAdapter(FacebookUserAdapter $userAdapter) {
		$this->userAdapter = $userAdapter;
	}

	public function setFacebook(Facebook $facebook) {
		$this->facebook = $facebook;
	}

}

?>